<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_计算器练习</title>
</head>
<body>
<input type="text" placeholder="请输入数字" id="i1"><br>
<input type="text" placeholder="请输入数字" id="i2"><br>
<button onclick="calc('+')">加</button>
<button onclick="calc('-')">减</button>
<button onclick="calc('*')">乘</button>
<button onclick="calc('/')">除</button>
<h4>运算结果：<span></span></h4>

<script>
    function calc(o){
        //获取用户输入的第一个数字
        let a1 = document.getElementById('i1').value;
        //获取用户输入的第二个数字
        let a2 = document.getElementById('i2').value;
        a1 = parseFloat(a1);
        a2 = parseFloat(a2);
        let spanE = document.querySelector('span')
        switch (o){
            case '+':
                spanE.innerHTML =a1 + a2;break;
            case '-':
                spanE.innerHTML =a1 - a2;break;
            case '*':
                spanE.innerHTML =a1 * a2;break;
            case '/':
                if (a2 != 0){
                    spanE.innerHTML =a1 / a2;
                }else{
                    alert('除数不能为0！');
                }
                break;
        }
    }
</script>
</body>
</html>